<template>
  <div class="authentication">
    <div class="title">
      <van-nav-bar
        title="实名认证"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
      />
    </div>
    <div class="tip">
      <div class="icon">
        <img src="../../assets/save.png" alt />
      </div>
      <p class="tip1">进⾏实名认证</p>
      <p class="tip2">⽤于保障账号的安全及快速接收订单信息</p>
    </div>

    <div class="form">
      <van-form>
        <van-field
          v-model="form.realname"
          name="真实姓名"
          label="真实姓名"
          placeholder="真实姓名"
          :rules="[{ required: true, message: '请填写真实姓名' }]"
        />
        <van-field
          v-model="form.idCard"
          name="身份证号"
          label="身份证号"
          placeholder="身份证号"
          :rules="[{ required: true, message: '请填写身份证号' }]"
        />
        <van-field
          v-model="form.bankCard"
          name="银⾏卡号"
          label="银⾏卡号"
          placeholder="银⾏卡号"
          :rules="[{ required: true, message: '请填写银⾏卡号' }]"
        />
        <van-field name="uploader" label="身份证正⾯照">
          <template #input>
            <van-uploader
              :after-read="afterRead1"
              v-model="fileList1"
              :max-count="1"
            />
          </template>
        </van-field>
        <van-field name="uploader" label="身份证反⾯照">
          <template #input>
            <van-uploader
              :after-read="afterRead2"
              v-model="fileList2"
              :max-count="1"
            />
          </template>
        </van-field>
        <van-field name="uploader" label="银⾏卡照⽚">
          <template #input>
            <van-uploader
              :after-read="afterRead3"
              v-model="fileList3"
              :max-count="1"
            />
          </template>
        </van-field>
        <div class="btn" @click="submitHandler">提交</div>
      </van-form>
    </div>
    <div style="height: 80px"></div>
  </div>
</template>

<script>
import { post_formData, post } from "../../http/axios";
import { Toast } from "vant";
export default {
  // 组件名称
  name: "demo",
  // 组件参数 接收来⾃⽗组件的数据
  props: {},
  // 组件状态值
  data() {
    return {
      form: {},
      fileList1: [],
      fileList2: [],
      fileList3: [],
    };
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件⽅法
  methods: {
    //返回上⼀⻚⾯
    onClickLeft() {
      this.$router.go(-1);
    },
    toBack() {
      this.$router.push({ path: "/writer/mine" });
    },
    afterRead1(file, detail) {
      // console.log(this.dataURLtoFileFun(file.content));
      let formData = new FormData();

      formData.append("file", this.dataURLtoFileFun(file.content, "file.jpg"));

      post_formData("http://121.199.29.84:8001/file/upload", formData).then(
        (res) => {
          console.log(res.data);
          if (res.data.status === 200) {
            this.form.idcardPhotoPositive =
              "http://121.199.29.84:8888/" +
              res.data.data.groupName +
              "/" +
              res.data.data.id;
            this.fileList1 = [
              {
                url:
                  "http://121.199.29.84:8888/" +
                  res.data.data.groupName +
                  "/" +
                  res.data.data.id,
              },
            ];
          }
        }
      );
    },
    afterRead2(file, detail) {
      console.log(this.dataURLtoFileFun(file.content));
      let formData = new FormData();

      formData.append("file", this.dataURLtoFileFun(file.content, "file.jpg"));

      post_formData("http://121.199.29.84:8001/file/upload", formData).then(
        (res) => {
          console.log(res.data);
          if (res.data.status === 200) {
            this.form.idcardPhotoNegative =
              "http://121.199.29.84:8888/" +
              res.data.data.groupName +
              "/" +
              res.data.data.id;
            this.fileList2 = [
              {
                url:
                  "http://121.199.29.84:8888/" +
                  res.data.data.groupName +
                  "/" +
                  res.data.data.id,
              },
            ];
          }
        }
      );
    },
    afterRead3(file, detail) {
      console.log(file);
      let formData = new FormData();

      formData.append("file", this.dataURLtoFileFun(file.content, "file.jpg"));

      post_formData("http://121.199.29.84:8001/file/upload", formData).then(
        (res) => {
          console.log(res.data);
          if (res.data.status === 200) {
            this.form.bankCardPhoto =
              "http://121.199.29.84:8888/" +
              res.data.data.groupName +
              "/" +
              res.data.data.id;
            this.fileList3 = [
              {
                url:
                  "http://121.199.29.84:8888/" +
                  res.data.data.groupName +
                  "/" +
                  res.data.data.id,
              },
            ];
          }
        }
      );
    },
    // bae64转⽂件对象
    dataURLtoFileFun(dataurl, filename) {
      // 将base64转换为⽂件，dataurl为base64字符串，filename为⽂件名（必须带后缀名，如.jpg,.png）
      let arr = dataurl.split(",");
      let mime = arr[0].match(/:(.*?);/)[1];
      let bstr = atob(arr[1]);
      let n = bstr.length;
      let u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new File([u8arr], filename, { type: mime });
    },
    //提交审核
    submitHandler() {
      this.form.userId = this.$route.query.userId;
      console.log(this.form);

      post("/certification/submitCertificationApply", this.form).then((res) => {
        console.log(res.data.status);
        if (res.data.status === 200) {
          Toast(res.data.message);
          this.$router.push("/manager/Mine");
        } else {
          Toast(res.data.message);
        }
      });
    },
  },
  // 以下是⽣命周期钩⼦
  /**
   * 组件实例创建完成，属性已绑定，但DOM还未⽣成，$ el属性还不存在
   */
  created() {},
  /**
 * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调⽤该钩⼦。
 * 如果 root 实例挂载了⼀个⽂档内元素，当 mounted 被调⽤时 vm.$
el 也在⽂档内。
 */
  mounted() {},
  /**
   * 实例销毁之前调⽤。在这⼀步，实例仍然完全可⽤。
   */
  beforeDestroy() {},
  /**
   * Vue 实例销毁后调⽤。调⽤后，Vue 实例指示的所有东⻄都会解绑定，
   * 所有的事件监听器会被移除，所有的⼦实例也会被销毁。
   */

  destroyed() {},
};
</script>

<style scoped >
.authentication.title {
  height: 58px;
  background-image: linear-gradient(to right, #bf73ff, #7579ff);
  text-align: center;
  line-height: 58px;
  color: #fff;
  letter-spacing: 0.2em;
}
.authentication .title span {
  float: left;
  margin-left: 1em;
  margin-top: 5px;
  cursor: pointer;
}
.tip .icon {
  width: 100px;
  height: 100px;
  margin: 0 auto;
  margin-top: 1em;
  border-radius: 50%;
  background-color: #ddfae7;
}
.tip .icon img {
  width: 98%;
  height: 98%;
}
.tip .tip1 {
  width: 50%;
  margin: 0 auto;
  text-align: center;
  margin-top: 1em;
  color: black;
  font-weight: 300;
}
.tip .tip2 {
  width: 80%;
  margin: 0 auto;
  text-align: center;
  margin-top: 1em;
  color: #999;
  font-weight: 300;
  font-size: 12px;
  letter-spacing: 0.1em;
}
.form {
  margin-top: 2em;
  padding: 0 0.5em;
}
.form .btn {
  height: 42px;
  background-image: linear-gradient(to right, #bf73ff, #7579ff);
  margin: 16px;
  border-radius: 5px;
  text-align: center;
  line-height: 42px;
  color: #fff;
  font-size: 14px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>